#include "MY_INC/APP/APP_ACLoad_Task.h" // 包含自己的头文件
#include "My_INC/APP/uart_process.h"
#include "My_INC/APP/device_handlers.h"
void task_20us(System *sys)
{
    if (sys->task.task_20us_flag == Nonactivated)
        return;
    sys->task.task_20us_flag = Nonactivated;
    //    LL_GPIO_ResetOutputPin(GPIO_TEST1_GPIO_Port, GPIO_TEST1_Pin);
}
void task_40us(System *sys)
{
    if (sys->task.task_40us_flag == Nonactivated)
        return;
    //    LL_GPIO_SetOutputPin(GPIO_TEST1_GPIO_Port, GPIO_TEST1_Pin);
    sys->task.task_40us_flag = Nonactivated;

    SignalAnalysisr_ProcessSample_Inline(sys->controller[0].param.SignalAnalysisr_v,
                                         sys->controller[0].param.vo_actual); // 检测有效值
    SignalAnalysisr_ProcessSample_Inline(sys->controller[1].param.SignalAnalysisr_v,
                                         sys->controller[1].param.vo_actual); // 检测有效值
    SignalAnalysisr_ProcessSample_Inline(sys->controller[1].param.SignalAnalysisr_i,
                                         sys->controller[1].param.il_actual); // 检测有效值
    //    LL_GPIO_ResetOutputPin(GPIO_TEST1_GPIO_Port, GPIO_TEST1_Pin);
}

void task_100us(System *sys)
{
    if (sys->task.task_100us_flag == Nonactivated)
        return;
    //    LL_GPIO_SetOutputPin(GPIO_TEST1_GPIO_Port, GPIO_TEST1_Pin);
    sys->task.task_100us_flag = Nonactivated;
    PQ_Process_Inline(sys->controller[1].param.pq, sys->controller[1].param.togi_v->D,
                      sys->controller[1].param.togi_v->Q, sys->controller[1].param.togi_i->D,
                      sys->controller[1].param.togi_i->Q);
    PF_Process_Inline(sys->controller[1].param.pq);
    //    LL_GPIO_ResetOutputPin(GPIO_TEST1_GPIO_Port, GPIO_TEST1_Pin);
}

volatile uint16_t voltage;
volatile uint16_t current;
volatile uint16_t Pe;
volatile uint16_t Qe;
volatile uint16_t FU;
volatile uint16_t PF;

void task_100ms(System *sys)
{
    if (sys->task.task_100ms_flag == Nonactivated)
        return;
    sys->task.task_100ms_flag = Nonactivated;
    //    LL_GPIO_TogglePin(GPIO_TEST1_GPIO_Port, GPIO_TEST1_Pin);
    voltage = (uint16_t)(sys->controller[1].param.SignalAnalysisr_v->Vrms * 100.0f);
    current = (uint16_t)(sys->controller[1].param.SignalAnalysisr_i->Vrms * 100.0f);
    Pe = (uint16_t)(sys->controller[1].param.pq->Pe_Disp * 100.0f);
    Qe = (uint16_t)(sys->controller[1].param.pq->Qe_Disp * 100.0f);
    FU = (uint16_t)(sys->controller[1].param.SignalAnalysisr_v->SigFreq * 100.0f);
    PF = (uint16_t)(sys->controller[1].param.pq->PF * 100.0f);
    static volatile uint8_t index;
    if (++index == 3)
        index = 0;
    switch (index)
    {
    case 0:
        /* code */
        send_realCurrentVoltage_command(voltage, current);
        break;
    case 1:
        /* code */
        send_realPQ_command(Pe, Qe);
        break;
    case 2:
        /* code */
        send_realFUPF_command(FU, PF);
        break;
    default:
        break;
    }

    sys->task.task_100ms_flag = Nonactivated;

    //    LL_GPIO_ResetOutputPin(GPIO_TEST1_GPIO_Port, GPIO_TEST1_Pin);
}
void task_400ms(System *sys)
{
    if (sys->task.task_400ms_flag == Nonactivated)
        return;
    sys->task.task_400ms_flag = Nonactivated;
    static volatile uint8_t index;
    if (++index == 3)
        index = 0;
    switch (index)
    {
    case 0:
        send_realSwitch_command((uint16_t)(sys->controller[0].loop_sm.current_state == STATE_RUN));
        break;
    case 1:
        send_realMainMode_command((uint16_t)(sys->wock_mode));
        break;
    case 2:
        send_realSubMode_command((uint16_t)(sys->device_mode));
        break;
    default:
        break;
    }
}
